<script>
import {
  profile,
  adddetailimg,
  deldetailimg,
  updateavatar,
  updateProfile,
} from "../../api/otherServer";
import Recommend from "../../components/recommend.vue";
import { Notify } from "vant";
import { mapGetters, mapActions, mapMutations } from "vuex";
import { statcUrl } from "../../api/api";
import staticurl from "../../assets/1_06.png";
import { get } from "vant/lib/utils";
export default {
  data() {
    return {
      mymoney: "",
      staticurl,
      show: false,
      fileList: [],
      statcUrl,
      value: "",
    };
  },
  computed: {
    ...mapGetters("usermessage", ["getProfile"]),
    updateUrl() {
      return statcUrl + "/" + this.getProfile.avatar;
    },
    myMoneys() {
      return Number(this.getProfile.mymoney).toFixed(2);
    },
    // 用户点击图片可以显示·
  },
  components: {
    recommend: Recommend,
  },
  mounted() {
    this.getProfiles();
    // this.setprofileActionsaddress({ data: [] });
  },
  methods: {
    ...mapActions("usermessage", [
      "setprofileActions",
      "setprofileActionsaddress",
    ]),
    showPopup() {
      this.show = true;
    },
    //添加图片的方法
    afterRead(file) {
      let form = new FormData(); //新建表单流数据
      form.append("file", file.file); //然后给表单里面添加file图片的信息,注意:因为表单对象里面的属性是私有的获取的话需要利用.get方法
      adddetailimg(form, (res) => {
        //数据库成功的添加了图片接下来就是让页面上成功的显示图片
        const { url } = res.data;
        updateavatar({ avatar: url }, (res) => {
          this.setprofileActions({ data: { ...this.getProfile, avatar: url } });
          this.fileList = [
            { url: statcUrl + "/" + url, isImage: true, name: url },
          ];
          console.log(this.fileList);
        });
      });
    },

    deletImg(file) {
      //file里面可以拿到当前服务器上的图片信息
      deldetailimg({ file: [file.name] }, (res) => {
        Notify({ type: "success", message: "删除成功" }); //提示删除成功在删除接口调用成功的时候
        updateavatar({ avatar: "" }, (res) => {
          //更新用户头像
          this.setprofileActions({ data: { ...this.getProfile, avatar: "" } }); //在es6中对象展开是会把对应的属性覆盖，而数组是追加
        });
      });
    },
    changeName() {
      if (this.value == this.getProfile.nick) {
        return;
      }
      updateProfile({ filedname: "nick", filedvalue: this.value }, () => {
        this.setprofileActions({
          data: { ...this.getProfile, nick: this.value },
        });
        this.show = false;
      });
    },
    getProfiles() {
      if (!this.getProfile.id) {
        profile((res) => {
          this.setprofileActions({ data: res.data[0][0] });

          console.log(res);
        });
      }
    },
    handelIMg() {
      this.show = "img";
      this.fileList = [
        {
          url: statcUrl + "/" + this.getProfile.avatar,
          isImage: true,
          name: this.getProfile.avatar,
        },
      ];
    },
    outlogin() {
      this.$router.push("/login");
      localStorage.clear(); //退出登录需要清理点，清理缓存的数据
      this.setprofileActions({ data: {} });
    },
  },
};
</script>
<template>
  <van-nav-bar title="我的" class="font_color" />
  <div class="bg_img">
    <div class="bg_img-div1">
      <figure class="user_img" @click="show = 'img'">
        <img
          @click="handelIMg"
          :src="getProfile.avatar ? updateUrl : staticurl"
          alt=""
        />
      </figure>
      <van-popup v-model:show="show" class="my-popup">
        <h1 style="font-size: 0.2rem">
          {{ this.show == "img" ? "修改用户头像" : "修改用户名" }}
        </h1>
        <van-uploader
          :after-read="afterRead"
          v-if="show == 'img'"
          v-model="fileList"
          :max-count="1"
          @delete="deletImg"
        />

        <template v-if="show === 'nick'">
          <van-cell-group inset>
            <van-field v-model="value" placeholder="请输入用户名" />
            <van-button type="primary" @click="changeName">确认修改</van-button>
          </van-cell-group>
        </template>
      </van-popup>
      <div class="infr_qian">
        <div class="infr_qian-div1">
          <span @click="show = 'nick'">{{ getProfile.nick }}</span
          ><span @click="outlogin">退出登录</span>
        </div>

        <div class="infr_qian-div2">
          <div>普通会员</div>
          <div>打卡签到</div>
        </div>
      </div>
    </div>
    <div class="bg_img-div2"><img src="../../assets/a_03.png" alt="" /></div>
  </div>
  <div class="person_ablity mt_bottom">
    <div class="person_ablity-item" @click="$router.push('/order0')">
      <span class="iconfont icon-31daifukuan"></span>
      <p>待付款</p>
    </div>
    <div class="person_ablity-item" @click="$router.push('/order1')">
      <span class="iconfont icon-31daifahuo"></span>
      <p>待发货</p>
    </div>
    <div class="person_ablity-item" @click="$router.push('/order2')">
      <span class="iconfont icon-31daishouhuo"></span>
      <p>待收货</p>
    </div>
    <div class="person_ablity-item" @click="$router.push('/order3')">
      <span class="iconfont icon-daipingjia"></span>
      <p>待评价</p>
      <div><img src="../../assets/my_icon_bg_03.png" alt="" /></div>
    </div>
    <div class="person_ablity-item">
      <span class="iconfont icon-wodedingdan"></span>
      <p>我的订单</p>
    </div>
  </div>
  <div class="person_ablity mt_bottom">
    <div class="person_ablity-item">
      <span class="font_color">{{ myMoneys }}</span>
      <p>待付款</p>
    </div>
    <div class="person_ablity-item">
      <span class="font_color">0</span>
      <p>待发货</p>
    </div>
    <div class="person_ablity-item">
      <span class="font_color">0</span>
      <p>待收货</p>
    </div>
    <div class="person_ablity-item">
      <span class="font_color">0</span>
      <p>待评价</p>
      <div><img src="../../assets/my_icon_bg_03.png" alt="" /></div>
    </div>
    <div class="person_ablity-item" @click="$router.push('/pay')">
      <span class="iconfont icon-wodedingdan font_color"></span>
      <p class="p-top">我的钱包</p>
    </div>
  </div>
  <ul class="container">
    <li>
      <img src="../../assets/a_03.png" alt="" />
      <p>足迹</p>
    </li>
    <li>
      <img src="../../assets/a_11.png" alt="" />
      <p>会员</p>
    </li>
    <li>
      <img src="../../assets/a_06.png" alt="" />
      <p>收藏</p>
    </li>
    <li>
      <img src="../../assets/a_08.png" alt="" />
      <p>分销</p>
    </li>
    <li>
      <img src="../../assets/a_21.png" alt="" />
      <p>消息</p>
    </li>
    <li>
      <img src="../../assets/a_18.png" alt="" />
      <p>设置</p>
    </li>
    <li>
      <img src="../../assets/a_26.png" alt="" />
      <p>客服</p>
    </li>
    <li>
      <img src="../../assets/a_24.png" alt="" />
      <p>帮助</p>
    </li>
  </ul>
  <recommend />
</template>
<style lang="scss" scoped>
.container {
  display: flex;
  width: 100%;
  background: #fff;
  text-align: center;
  padding: 0 0.4rem;
  flex-wrap: wrap;
  padding-bottom: 0.33rem;
  margin-bottom: 0.2rem;
  li {
    flex: 0 0 25%;
    img {
      width: 0.63rem;
    }
    p {
      font-size: 0.24rem;
    }
  }
}
.bg_img {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 1.95rem;
  background-image: url("../../assets/user-bg.png");
  background-repeat: no-repeat;
  background-size: cover;
  background-color: #fff;
  padding: 0.5rem 0.22rem;
  .bg_img-div1 {
    display: flex;
  }
}

.bg_img-div2 img {
  width: 1.2rem;
}
.user_img {
  img {
    width: 1.2rem;
    // height: 0.98rem;
  }
}
.infr_qian {
  color: #fff;
  margin-left: 0.22rem;
}
.infr_qian-div1 {
  font-size: 0.24rem;
  margin-bottom: 0.2rem;
}
.infr_qian-div1 span:nth-child(1) {
  font-size: 0.32rem;
}
.infr_qian-div1 span:nth-child(2) {
  margin-top: 0.16rem;
  margin-left: 0.3rem;
}
.infr_qian-div2 {
  font-size: 0.2rem;
  div {
    display: inline-block;
    width: 1.1rem;
    height: 0.5rem;
    line-height: 0.45rem;
    text-align: center;
    border: 1px solid #fff;
  }
}
.infr_qian-div2 div:nth-child(2) {
  margin-left: 0.35rem;
}
.mt_bottom {
  margin-bottom: 0.2rem;
}
.person_ablity {
  background-color: #fff;
  display: flex;
  width: 100%;
  // height: 1.53rem;
  padding: 0.4rem 0.28rem 0.29rem 0.37rem;
  .person_ablity-item:nth-child(4) {
    position: relative;
    & > div {
      position: absolute;
      right: -0.06rem;
      top: 0;
      img {
        height: 0.83rem;
      }
    }
  }
  .person_ablity-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 0 0 20%;
    text-align: center;
    height: 0.95rem;
    .font_color {
      color: #fe6c71;
    }
    span {
      color: #e3ba68;
      font-size: 0.32rem;
    }
    p {
      color: #707070;
      font-size: 0.24rem;
      margin-top: 0.22rem;
    }
    .p-top {
      margin-top: 0.27rem;
    }
  }
}
</style>
<style src="./index.scss"></style>
